<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Lib\Response;
use App\Http\Model\Hint;
use App\Http\Services\MsgServices;
use App\Http\Services\WishWheelServices;

//波波球
class ApiWishWheelController extends BaseController
{
    protected $wishWheelService;

    public function __construct(){
        $this->wishWheelService = new WishWheelServices();
    }

    /**
     * 获取单次抽奖价格
     */
    public function wishWheelPrice(){
        $luckId = 1000;
        Response::returnResult(Hint::SUCCESS, ['price' => 200]);
    }

    /**
     * 波波球奖池配置获取
     */
    public function wishWheelConfig(Request $request)
    {
        $userId = $this->getLoginUserId();

        $data = $this->wishWheelService->wishWheelConfig($userId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 波波球奖品列表
     */
    public function wishWheelGiftList(Request $request)
    {
        $userId = $this->getLoginUserId();

        $data = $this->wishWheelService->wishWheelGiftList($userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }
    
    /**
     * 自动兑换告白币提示
     * 
     */
    public function wishWheelPop(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $num = (int) $request->input("num");
        $roomId = (int) $request->input("room_id");
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }

        $data = $this->wishWheelService->wishWheelPop($userId, $num, $roomId);

        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 确认弹窗
     */
    public function wishWheelConfirm(Request $request){
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $data = $this->wishWheelService->wishWheelConfirm($userId);

        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 波波球抽奖
     */
    public function wishWheelDraw(Request $request)
    {
        $userId = $this->getLoginUserId();
        if(empty($userId)) {
            Response::returnResult(Hint::LOGIN_OUT);
        }
        $num = (int) $request->input("num");
        $roomId = (int) $request->input("room_id");
        if($num < 1) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        // if(!in_array($userId, [10000, 10001])) {
        //     Response::returnResult(Hint::PARAM_ERROR, [], '10:30-11:30系统维护，请稍后');
        // }

        //锁
        $key = 'WISHWHEEL:wishWheelDraw:'.$userId;
        $ifLock = redis_lock($key, 3600);
        if (!$ifLock){
            Response::returnResult(Hint::SAME_TIME_NOT_REPEAT, [], '抽奖中, 同一时间请勿重复请求');
        }

        $data = $this->wishWheelService->wishWheelDraw($userId, $num, $roomId);
        redis_unlock($key);

        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 波波球发飘屏
     */
    public function wishWheelDrawFull(Request $request)
    {
        $orderId = (int) $request->input('order_id', 0);
        if(empty($orderId)) {
            Response::returnResult(Hint::PARAM_ERROR);
        }
        //锁
        $ifLock = redis_lock('WISHWHEEL:wishWheelDrawFull:'.$orderId, 60);
        if (!$ifLock){
            // Response::returnResult(Hint::SAME_TIME_NOT_REPEAT);
        }

        $data = $this->wishWheelService->wishWheelDrawFull($orderId);
        if($data['code'] != Hint::SUCCESS) {
            Response::returnResult($data['code'], $data['data'], $data['msg']);
        }
        Response::returnResult(Hint::SUCCESS, $data['data']);
    }

    /**
     * 波波球用户中奖记录列表
     */
    public function wishWheelAwardListByUserId(Request $request)
    {
        $limit = (int) $request->input('page_size', 10);
        $orderId = (int) $request->input('order_id', 0);
        $userId = $this->getLoginUserId();

        $data = $this->wishWheelService->wishWheelAwardListByUserId($orderId, $limit, $userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }

    /**
     * 波波球今日中奖记录列表
     */
    public function wishWheelDrawAwardListV2(Request $request)
    {
        $limit = (int) $request->input('page_size', 10);
        $page = (int) $request->input('page_index', 0);
        $userId = $this->getLoginUserId();

        $data = $this->wishWheelService->wishWheelDrawAwardListV2($page, $limit, $userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }
    
    /**
     * 波波球今日中奖记录列表
     */
    public function wishWheelDrawAwardList(Request $request)
    {
        $limit = (int) $request->input('page_size', 10);
        $orderId = (int) $request->input('order_id', 0);
        $userId = $this->getLoginUserId();

        $data = $this->wishWheelService->wishWheelDrawAwardList($orderId, $limit, $userId);

        Response::returnResult(Hint::SUCCESS, $data);
    }





}
